<?php
/*Problem Domain
To perform the actual CRUD operations on UserSession by implementing the UserSessionDAO interface
*/
/*
include_once 'UserSessionDAO.php';
include_once '../model/UserSession.php';
include_once '../model/ListUserType.php';
*/
class UserSessionDAOImpl implements UserSessionDAO
{
	private $conn;
	private $array_list;

	/*void 
	purpose. initializes the newly created UserSessionDAOImpl.*/
	public function __construct()
	{
		$this->conn = new mysqli('localhost' , 'root' , 'root123' , 'tms') or
		die('Connect error : '.$this->conn->connect_error);

		$this->array_list = new ListUserType();
	}

	/*void -> Connection
	purpose. produces the connection object*/
	public function getConnection()
	{
		return $this->conn;
	}

	/*User -> boolean
	purpose. produces true if the given UserSession is found , false otherwise*/
	private function found($usersession)
	{
		$conn = $this->getConnection();
		$id = $usersession->getId();
		$res = $conn->query("select * from User_Session where id = ".$id);
		$status;
		
		if($res->num_rows == 0)
		{
			$status = false;
		}
		else
		{
			$status = true;
		}

		return $status;
	}

	/*User -> boolean
	purpose. To persist the given UserSession into the database and return true on success
	false otherwise*/
	private function save($usersession)
	{
		$conn 	    = $this->getConnection();
		$user_id    = $usersession->getUserId();
	   	$session_id = $usersession->getSessionId();
		$sts	    = $usersession->getStatus();
		
		$stmt = $conn->prepare('insert into User_Session(`user_id`,`sess_id`,`status`) values(? , ? , ? )');
		$status = true;

		if(!$stmt)
		{
			echo "\n Prepared Statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('iis', $user_id, $session_id, $sts))
			{
				echo "Bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
				else
				{
					$id = $conn->insert_id;
					$usersession->setId($id);
				}
			}
		}

		return $status;		
	}

	/*User -> boolean
	pupose. To update the already existing given UserSession and return true on success , false otherwise.*/
	public function update($usersession)
	{
		$conn     = $this->getConnection();
		$id  	  = $usersession->getId();
		$sts	  = $usersession->getStatus();
		$reason   = $usersession->getReason();	
		$status   = true;
		$stmt     = $conn->prepare('update User_Session set `status` = ? , `reason` = ? where `id` = ?');

		if(!$stmt)
		{
			echo "Prepare statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('ssi', $sts, $reason, $id))
			{
				echo "Parameter bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
			}
		}

		return $status;
	}

	/*User -> boolean
        purpose. to save or update a given User into the database and return true on success , 
        false otherwise*/
        public function saveOrUpdate($User)
	{
		$status;

		if(!$this->found($User))
		{
			$status = $this->save($User);			
		}
		else
		{
			$status = $this->update($User);
		}

		return $status;
	}	

	/*User -> boolean
        purpose. removes a given usersession from the database and returns true
        on succes , false otherwise*/
        public function remove($usersession)
	{
		$conn = $this->getConnection();
		$status = true;

		if(!$this->found($usersession))
		{
			echo "\n".$usersession->getId()."is not found";
			$status = false;
		}
		else
		{
			$id	= $usersession->getId();
			$sql    = "delete from User_Session where `id` = ".$id;

			if(!$conn->query($sql))
			{
				echo "Query Execution error no : ".$conn->errno."error : ".$conn->error;
				$status = false;
			}
		}

		return $status;
	}

	/*void -> ListUserType
	purpose. produces the entire list of UsersSession Mapping of the system*/
	public function listUserSession()
	{
		$conn = $this->getConnection();
		$sql = 'select * from User_Session';
		$res = $conn->query($sql);

		while($row = $res->fetch_assoc())
		{
			$id	 	= $row['id'];
			$user_id 	= $row['user_id'];
			$sess_id	= $row['sess_id'];
			$sts		= $row['status'];
			$reason		= $row['reason'];
			
			$usersession	= new UserSession($user_id, $sess_id, $sts, $reason);

			$this->array_list->addToList($usersession);
		}

		return $this->array_list;
	}
}
?>
